package com.douyang.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.douyang.VO.GetLivePeople;
import com.douyang.VO.MessageVO;
import com.douyang.VO.UserInfoVO;
import com.douyang.dto.LoginDTO;
import com.douyang.base.model.Result;
import com.douyang.base.model.UserBaseInfo;
import com.douyang.dto.SendMessageDTO;
import com.douyang.service.UserBaseInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import reactor.core.publisher.Flux;

import java.io.PrintWriter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/user/userBaseInfo")

public class UserBaseInfoController {
    @Autowired
    private UserBaseInfoService userBaseInfoService;

    @PostMapping("/doLogin")
    public Result<String> doLogin(@RequestBody @Validated LoginDTO loginDTO){
        UserBaseInfo userBaseInfo=userBaseInfoService.doLogin(loginDTO);
        StpUtil.login(userBaseInfo.getId());
        StpUtil.getSession().set("userBaseInfo", userBaseInfo);
        return Result.success(StpUtil.getTokenValue());
    }

    @GetMapping("/getCode")
    public Result<String> getCode(HttpServletRequest request,String email){
        userBaseInfoService.getCode(request,email);
        return Result.success("ok");
    }

    @GetMapping("/logout")
    public Result<String> doLogout(){
        StpUtil.logout();
        return Result.success("ok");
    }

    @GetMapping("/checkLogin")
    public Result<Boolean> checkLogin(){
        return Result.success(StpUtil.isLogin());
    }

    @GetMapping("/getUserInfo")
    public Result<UserInfoVO> getUserInfo(Long userId){
        return Result.success(userBaseInfoService.getUserInfo(userId));
    }

    @GetMapping("/getMessageList")
    public Result<List<MessageVO>> getMessageList(){
        return Result.success(userBaseInfoService.getMessageList());
    }

    @PostMapping("/sendMessage")
    public Result<MessageVO> sendMessage(@RequestBody @Validated SendMessageDTO sendMessageDTO){
        return Result.success(userBaseInfoService.sendMessage(sendMessageDTO));
    }


    @GetMapping("/getAnswer/{model}")
    public Flux<String> getAIAnswerByStream(@PathVariable("model") String model){
        return userBaseInfoService.getAIAnswerByStream(model);
    }

    @GetMapping("/clearChat")
    public Result<String> clearChat(){
        userBaseInfoService.clearChat();
        return Result.success("ok");
    }


}
